<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="车间编码" prop="code">
                <el-input v-model.trim="queryParams.code" placeholder="请输入车间编码" style="width: 200px;"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="车间名称" prop="name">
                <el-input v-model.trim="queryParams.name" placeholder="请输入车间名称" style="width: 200px;"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="负责人" prop="chargePerson">
                <el-input v-model.trim="queryParams.chargePerson" placeholder="请输入负责人" style="width: 200px;"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="科目" prop="subjectType">
                <el-select v-model="queryParams.subjectType" style="width: 200px;" placeholder="请选择科目">
                    <el-option v-for="item in ems_workshop_subject_type" :key="item.value" :value="item.value"
                        :label="item.label">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="印刷种类" prop="printType">
                <el-select v-model="queryParams.printType" style="width: 200px;" placeholder="请选择印刷种类">
                    <el-option v-for="item in ems_workshop_print_type" :key="item.value" :value="item.value"
                        :label="item.label">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" border :data="dataList">
            <el-table-column label="序号" width="60" type="index" align="center" />
            <el-table-column label="状态" align="center" :show-overflow-tooltip="true" prop="status"
                v-if="columns[0].visible">
                <template v-slot="{ row }">
                    <el-tag :type="row.status == 1 ? 'danger' : ''" effect="dark"> {{ row.status == 1 ? '停用' : "启用" }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="车间编号" min-width="120" align="center" :show-overflow-tooltip="true" prop="code"
                v-if="columns[1].visible">
                <template v-slot="{ row }">
                    <el-button type="primary" @click="handleView(row)" link>{{ row.code
                    }}</el-button>
                </template>
            </el-table-column>
            <el-table-column label="车间名称" align="center" :show-overflow-tooltip="true" prop="name"
                v-if="columns[2].visible">
            </el-table-column>
            <el-table-column label="印刷种类" align="center" :show-overflow-tooltip="true" prop="printType"
                v-if="columns[3].visible">
                <template v-slot="{ row }">
                    <dict-tag :options="ems_workshop_print_type" :value="row.printType" />
                </template>
            </el-table-column>
            <el-table-column label="科目" min-width="120" align="center" :show-overflow-tooltip="true" prop="subjectType"
                v-if="columns[4].visible">
                <template v-slot="{ row }">
                    <dict-tag :options="ems_workshop_subject_type" :value="row.subjectType" />
                </template>
            </el-table-column>
            <el-table-column label="负责人" align="center" :show-overflow-tooltip="true" prop="chargePerson"
                v-if="columns[5].visible">
            </el-table-column>
            <el-table-column label="负责人电话" align="center" :show-overflow-tooltip="true" prop="chargePersonTel"
                v-if="columns[6].visible">
            </el-table-column>

            <el-table-column label="备注" align="center" :show-overflow-tooltip="true" prop="remark"
                v-if="columns[7].visible">
            </el-table-column>
            <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-tooltip content="修改" placement="top" v-if="scope.row.status == 1">
                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top" v-if="scope.row.status == 1">
                        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
                    </el-tooltip>
                    <el-button link type="primary" icon="TurnOff" @click="handleService(scope.row)">{{ scope.row.status == 1
                        ? '启用' : '停用' }}</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script setup name="workshop">
import { downLoad } from '@/utils/ruoyi'
import { getMesWorkShop, delMesWorkShop, editMesWorkShop, exportMesWorkShop } from '@/api/basicInfo/workshop'
import { ElMessage, ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([])
const showSearch = ref(true);

const total = ref(0);
const router = useRouter()
const { ems_workshop_print_type, ems_workshop_subject_type } = proxy.useDict("ems_workshop_print_type", "ems_workshop_subject_type");

// 列显隐信息
const columns = ref([
    { key: 0, label: `状态`, visible: true },
    { key: 1, label: `车间编号`, visible: true },
    { key: 2, label: `车间名称`, visible: true },
    { key: 3, label: `印刷种类`, visible: true },
    { key: 4, label: `科目`, visible: true },
    { key: 5, label: `负责人`, visible: true },
    { key: 6, label: `负责人电话`, visible: true },
    { key: 7, label: `备注`, visible: true },
]);

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code: undefined,
        status: undefined
    },
});
const { queryParams } = toRefs(data);

const getList = () => {
    loading.value = true
    getMesWorkShop(queryParams.value).then(res => {
        loading.value = false
        dataList.value = res.rows
        total.value = res.total
    }).catch(err => {
        loading.value = false
    })
}
// 新增
const handleAdd = () => {
    router.push('/basicInfo/workshop/details')
}
// 编辑 
const handleUpdate = (row) => {
    router.push({
        path: '/basicInfo/workshop/details',
        query: {
            id: row.id,
        }
    })
}
// 查看
const handleView = (row) => {
    router.push({
        path: '/basicInfo/workshop/details',
        query: {
            id: row.id,
            info: true
        }
    })
}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
    proxy.$modal.confirm('是否确认删除"' + row.name + '"的数据项？').then(function () {
        return delMesWorkShop({ id: row.id });
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
};
//停用启用
const handleService = (row) => {
    const state = row.status === 1 ? '2' : '1';
    const stateText = row.status === 1 ? '启用' : '停用';
    ElMessageBox.confirm(`确定要${stateText}名称为「${row.name}」的车间信息吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        editMesWorkShop({ id: row.id, status: state, type: 1 }).then(response => {
            if (response.code === 200) {
                ElMessage({
                    message: `${stateText}成功`,
                    type: 'success',
                });
                getList();
            }
        });
    })
};
// 导出
const handleExport = () => {
    exportMesWorkShop(queryParams.value).then(res => {
        downLoad(res.msg, '车间列表')
    })
}
getList()
</script>

<style lang="scss" scoped></style>